/**
 * File : TokenUtils.java <br/>
 * Author : rising.xin <br/>
 * Version : 1.1 <br/>
 * Date : 2017年2月18日 <br/>
 * Modify : <br/>
 * Package Name : com.zhongpin.zp.common.util.token <br/>
 * Project Name : zp-common <br/>
 * Description : <br/>
 * 
 */

package com.yuju.util;

import java.security.SecureRandom;

import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;

/**
 * ClassName : TokenUtils <br/>
 * Function : TokenUtils. <br/>
 * Reason : TokenUtils. <br/>
 * Date : 2017年2月18日 上午9:06:55 <br/>
 * 
 * @author : rising.xin <br/>
 * @version : 1.1 <br/>
 * @since : JDK 1.6 <br/>
 * @see
 */

public class TokenUtils {

	private static final String TOKEN_SECURE_KEY = "com.yuju";

	/**
	 * 根据内容 生成一个 token
	 * 
	 * @param content
	 * @return
	 */
	public static String create(String content) {
		try {
			content += ("_" + System.currentTimeMillis());
			KeyGenerator kgen = KeyGenerator.getInstance("AES");
			SecureRandom secureRandom = SecureRandom.getInstance("SHA1PRNG");
			secureRandom.setSeed(TOKEN_SECURE_KEY.getBytes("UTF-8"));
			kgen.init(128, secureRandom);
			SecretKey secretKey = kgen.generateKey();
			byte[] enCodeFormat = secretKey.getEncoded();
			SecretKeySpec secKey = new SecretKeySpec(enCodeFormat, "AES");
			Cipher cipher = Cipher.getInstance("AES");// 创建密码器
			byte[] byteContent = content.getBytes("UTF-8");
			cipher.init(Cipher.ENCRYPT_MODE, secKey);// 初始化
			byte[] result = cipher.doFinal(byteContent);
			return byte2hex(result);
		}catch (Exception e) {
		}
		return null;
	}

	/**
	 * 根据token 解析token里面的内容
	 * 
	 * @param content
	 * @return
	 */
	public static String parse(String token) {
		try {
			KeyGenerator kgen = KeyGenerator.getInstance("AES");
			SecureRandom secureRandom = SecureRandom.getInstance("SHA1PRNG");
			secureRandom.setSeed(TOKEN_SECURE_KEY.getBytes("UTF-8"));
			kgen.init(128, secureRandom);
			SecretKey secretKey = kgen.generateKey();
			byte[] enCodeFormat = secretKey.getEncoded();
			SecretKeySpec secKey = new SecretKeySpec(enCodeFormat, "AES");
			Cipher cipher = Cipher.getInstance("AES");// 创建密码器
			cipher.init(Cipher.DECRYPT_MODE, secKey);// 初始化
			byte[] hexByte = hex2byte(token);
			byte[] result = cipher.doFinal(hexByte);
			String content = new String(result, "UTF-8"); 
			if(null != content && content.contains("_")){
				return content.split("_")[0]; 
			}
		}catch (Exception e) {
		}
		return null;
	}

	/**
	 * byte 数组转字符串
	 * 
	 * @param b
	 * @return
	 */
	private static String byte2hex(byte[] b) {
		String hs = "";
		String stmp = "";
		for (int n = 0; n < b.length; n++) {
			stmp = (java.lang.Integer.toHexString(b[n] & 0XFF));
			if (stmp.length() == 1) {
				hs = hs + "0" + stmp;
			}else {
				hs = hs + stmp;
			}
		}
		return hs.toUpperCase();
	}

	/**
	 * 字符串转byte数组
	 * 
	 * @param hex
	 * @return
	 */
	private static byte[] hex2byte(String hex) {
		if (hex == null || hex.isEmpty() || hex.length() % 2 != 0) {
			return new byte[0];
		}
		byte[] ret = new byte[hex.length() / 2];
		byte[] tmp = hex.getBytes();
		for (int i = 0; i < hex.length() / 2; i++) {
			ret[i] = uniteBytes(tmp[i * 2], tmp[i * 2 + 1]);
		}
		return ret;
	}

	/**
	 * 两个数组合并
	 * 
	 * @param src0
	 * @param src1
	 * @return
	 */
	private static byte uniteBytes(byte src0, byte src1) {
		byte _b0 = Byte.decode("0x" + new String(new byte[]{ src0 })).byteValue();
		_b0 = (byte) (_b0 << 4);
		byte _b1 = Byte.decode("0x" + new String(new byte[]{ src1 })).byteValue();
		byte ret = (byte) (_b0 ^ _b1);
		return ret;
	}

}
